package com.androidx.p013lv.invention.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidx.p013lv.base.APPLink;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.AdInfoBean;
import com.androidx.p013lv.base.bean.LikeVideoBean;
import com.androidx.p013lv.base.bean.UserAccount;
import com.androidx.p013lv.base.bean.VideoBean;
import com.androidx.p013lv.base.bean.VideoListBean;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.model.VideoModel;
import com.androidx.p013lv.base.model.VideoPlayerModel;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.service.AdClickService;
import com.androidx.p013lv.base.utils.AdUtils;
import com.androidx.p013lv.base.utils.LogUtils;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.invention.C1069R;
import com.androidx.p013lv.invention.adapter.AdapterVideo;
import com.androidx.p013lv.invention.databinding.FragmentVideoBinding;
import com.androidx.p013lv.player.view.BrushVideoPlayer;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yalantis.ucrop.view.CropImageView;
import java.util.ArrayList;
import java.util.Objects;

/* renamed from: com.androidx.lv.invention.fragment.VideoFragment */
public class VideoFragment extends LazyFragment<FragmentVideoBinding> implements OnRefreshListener, OnLoadMoreListener, BrushVideoPlayer.PlayerVideoInterface {
    AdapterVideo adapter;
    APPLink appLink;
    int count = 0;
    VideoBean data;
    private int firstVisibleItem;
    int index;
    boolean isOnClick = false;
    boolean isResume = true;
    int itemHeight;
    private int lastVisibleItem;
    private LinearLayoutManager linearLayoutManager;
    VideoModel model;
    VideoPlayerModel modelPlayer;
    private int page = 1;
    int perice;
    private BrushVideoPlayer player;
    int position;
    int top = 0;
    TextView tv_collect;
    UserAccount userAccount;
    BrushVideoPlayer videoPlayer;
    private int visibleCount;

    static /* synthetic */ int access$008(VideoFragment videoFragment) {
        int i = videoFragment.page;
        videoFragment.page = i + 1;
        return i;
    }

    static /* synthetic */ int access$508(VideoFragment videoFragment) {
        int i = videoFragment.visibleCount;
        videoFragment.visibleCount = i + 1;
        return i;
    }

    public static VideoFragment newInstance(VideoBean videoBean) {
        Bundle bundle = new Bundle();
        VideoFragment videoFragment = new VideoFragment();
        bundle.putSerializable("data", videoBean);
        videoFragment.setArguments(bundle);
        return videoFragment;
    }

    @Override // androidx.fragment.app.Fragment
    public void setArguments(Bundle bundle) {
        super.setArguments(bundle);
        this.data = (VideoBean) bundle.getSerializable("data");
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public int setFragmentView() {
        return C1069R.C1074layout.fragment_video;
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onResume() {
        super.onResume();
        this.isResume = true;
        this.userAccount = SpUtils.getInstance().getUserAccount();
        BrushVideoPlayer brushVideoPlayer = this.player;
        if (brushVideoPlayer != null) {
            int currentState = brushVideoPlayer.getCurrentState();
            LogUtils.m63e("huilaid", "--->" + currentState);
            if (currentState == 5) {
                this.player.onVideoResume();
            } else {
                this.player.startPlayLogic();
            }
        } else {
            AdapterVideo adapterVideo = this.adapter;
            if (adapterVideo == null || adapterVideo.getItemCount() <= 0) {
                this.adapter.setIsPlay(1);
            } else {
                autoPlayVideo(((FragmentVideoBinding) this.binding).recycler);
            }
        }
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onPause() {
        super.onPause();
        this.isResume = false;
        BrushVideoPlayer brushVideoPlayer = this.player;
        if (brushVideoPlayer != null && brushVideoPlayer.getCurrentState() != 0) {
            this.player.onVideoPause();
        }
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onDestroy() {
        super.onDestroy();
        VideoModel videoModel = this.model;
        if (videoModel != null) {
            videoModel.cancelHttp();
        }
        VideoPlayerModel videoPlayerModel = this.modelPlayer;
        if (videoPlayerModel != null) {
            videoPlayerModel.cancelHttp();
        }
        this.player = null;
    }

    @Override // com.gyf.immersionbar.components.ImmersionOwner, com.androidx.p013lv.base.p015ui.LazyFragment
    public void initImmersionBar() {
        ImmersionBar.with(this).statusBarDarkFont(false).titleBar(((FragmentVideoBinding) this.binding).toolbar).init();
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public void initViews() {
        this.model = (VideoModel) new ViewModelProvider(this).get(VideoModel.class);
        this.modelPlayer = (VideoPlayerModel) new ViewModelProvider(this).get(VideoPlayerModel.class);
        ((FragmentVideoBinding) this.binding).refresh.setOnRefreshListener(this);
        ((FragmentVideoBinding) this.binding).refresh.setOnLoadMoreListener(this);
        this.linearLayoutManager = new LinearLayoutManager(getContext());
        ((FragmentVideoBinding) this.binding).recycler.setLayoutManager(this.linearLayoutManager);
        this.adapter = new AdapterVideo(getActivity(), this);
        ((FragmentVideoBinding) this.binding).recycler.setAdapter(this.adapter);
        ((FragmentVideoBinding) this.binding).recycler.setHasFixedSize(true);
        ((FragmentVideoBinding) this.binding).statusLayout.setOnRetryListener(new View.OnClickListener() {
            /* class com.androidx.p013lv.invention.fragment.VideoFragment.View$OnClickListenerC11521 */

            public void onClick(View view) {
                VideoFragment.this.page = 1;
                VideoFragment.this.getInfo();
            }
        });
        this.adapter.setItemListener(new ItemClickListener() {
            /* class com.androidx.p013lv.invention.fragment.VideoFragment.C11532 */

            @Override // com.androidx.p013lv.base.listener.ItemClickListener
            public void onItemClick(View view, int i) {
                if (view.getId() == C1069R.C1072id.tv_collect) {
                    VideoFragment.this.tv_collect = (TextView) view;
                    VideoFragment.this.position = i;
                    VideoBean videoBean = (VideoBean) VideoFragment.this.adapter.getDataInPosition(i);
                    if (videoBean.isLikeBrush()) {
                        VideoFragment.this.model.unLikeVideo(videoBean.getVideoId(), VideoFragment.this.getContext());
                    } else {
                        VideoFragment.this.model.likeVideo(videoBean.getVideoId(), VideoFragment.this.getContext());
                    }
                } else if (view.getId() == C1069R.C1072id.tv_share) {
                    ARouter.getInstance().build(Constance.SHARE_ACTIVITY).withInt(Key.VIDEO_ID, ((VideoBean) VideoFragment.this.adapter.getDataInPosition(i)).getVideoId()).navigation();
                } else if (view.getId() == C1069R.C1072id.tv_download) {
                    VideoFragment videoFragment = VideoFragment.this;
                    videoFragment.startAd(((VideoBean) videoFragment.adapter.getDataInPosition(i)).getAdInfoBean());
                }
            }
        });
        this.model.getLikeVideo().observe(this, new Observer<BaseRes<LikeVideoBean>>() {
            /* class com.androidx.p013lv.invention.fragment.VideoFragment.C11543 */

            public void onChanged(BaseRes<LikeVideoBean> baseRes) {
                if (baseRes.getCode() == 200) {
                    ((VideoBean) VideoFragment.this.adapter.getDataInPosition(VideoFragment.this.position)).setLikeBrush(true);
                    ((VideoBean) VideoFragment.this.adapter.getDataInPosition(VideoFragment.this.position)).addFakeLikes();
                    if (VideoFragment.this.tv_collect != null) {
                        TextView textView = VideoFragment.this.tv_collect;
                        textView.setText(UiUtils.num2str(((VideoBean) VideoFragment.this.adapter.getDataInPosition(VideoFragment.this.position)).getFakeLikes()) + "");
                        VideoFragment.this.tv_collect.setCompoundDrawablesWithIntrinsicBounds(VideoFragment.this.tv_collect.getContext().getResources().getDrawable(C1069R.C1071drawable.ic_collect), (Drawable) null, (Drawable) null, (Drawable) null);
                    }
                    ToastUtils.getInstance().showCorrect("收藏成功");
                    return;
                }
                ToastUtils.getInstance().showWeak("收藏失敗");
            }
        });
        this.model.getUnLikeVideo().observe(this, new Observer<BaseRes>() {
            /* class com.androidx.p013lv.invention.fragment.VideoFragment.C11554 */

            public void onChanged(BaseRes baseRes) {
                if (baseRes.getCode() != 200) {
                    ToastUtils.getInstance().showWeak("取消收藏失敗");
                } else if (VideoFragment.this.adapter.getData().size() != 0) {
                    ((VideoBean) VideoFragment.this.adapter.getDataInPosition(VideoFragment.this.position)).setLikeBrush(false);
                    ((VideoBean) VideoFragment.this.adapter.getDataInPosition(VideoFragment.this.position)).minusFakeLikes();
                    if (VideoFragment.this.tv_collect != null) {
                        TextView textView = VideoFragment.this.tv_collect;
                        textView.setText(UiUtils.num2str(((VideoBean) VideoFragment.this.adapter.getDataInPosition(VideoFragment.this.position)).getFakeLikes()) + "");
                        VideoFragment.this.tv_collect.setCompoundDrawablesWithIntrinsicBounds(VideoFragment.this.tv_collect.getContext().getResources().getDrawable(C1069R.C1071drawable.ic_collect_no), (Drawable) null, (Drawable) null, (Drawable) null);
                    }
                    ToastUtils.getInstance().showCorrect("取消收藏成功");
                }
            }
        });
        this.model.getLiveData().observe(this, new Observer<BaseRes<VideoListBean>>() {
            /* class com.androidx.p013lv.invention.fragment.VideoFragment.C11565 */

            public void onChanged(BaseRes<VideoListBean> baseRes) {
                ((FragmentVideoBinding) VideoFragment.this.binding).statusLayout.hideLoading();
                ((FragmentVideoBinding) VideoFragment.this.binding).refresh.finishRefresh();
                ((FragmentVideoBinding) VideoFragment.this.binding).refresh.finishLoadMore();
                if (baseRes.getCode() == 200) {
                    if (baseRes.getData() != null && baseRes.getData().getData() != null && baseRes.getData().getData().size() > 0) {
                        if (VideoFragment.this.data != null && VideoFragment.this.page == 1) {
                            baseRes.getData().getData().add(0, VideoFragment.this.data);
                        }
                        int ruleIntervalNum = AdUtils.getInstance().getRuleIntervalNum();
                        if (AdUtils.getInstance().getVideoAd() != null) {
                            for (int i = 0; i < baseRes.getData().getData().size(); i++) {
                                if (VideoFragment.this.count == ruleIntervalNum) {
                                    AdInfoBean videoAd = AdUtils.getInstance().getVideoAd();
                                    VideoBean videoBean = new VideoBean();
                                    videoBean.setAd(true);
                                    ArrayList arrayList = new ArrayList();
                                    arrayList.add(videoAd.getAdImage());
                                    videoBean.setCoverImg(arrayList);
                                    if (videoAd.getAdType().equals("VIDEO")) {
                                        videoBean.setVideoUrl(videoAd.getAdPlay());
                                    }
                                    videoBean.setAdInfoBean(videoAd);
                                    baseRes.getData().getData().add(i, videoBean);
                                    VideoFragment.this.count = 0;
                                } else {
                                    VideoFragment.this.count++;
                                }
                            }
                        }
                        if (VideoFragment.this.page == 1) {
                            VideoFragment.this.adapter.setDomain(baseRes.getData().getDomain());
                            VideoFragment.this.adapter.setData(baseRes.getData().getData());
                            ((FragmentVideoBinding) VideoFragment.this.binding).refresh.resetNoMoreData();
                        } else {
                            VideoFragment.this.adapter.setDatasInEnd(baseRes.getData().getData());
                        }
                        VideoFragment.access$008(VideoFragment.this);
                    } else if (VideoFragment.this.page == 1) {
                        ((FragmentVideoBinding) VideoFragment.this.binding).statusLayout.showEmpty();
                    } else {
                        ((FragmentVideoBinding) VideoFragment.this.binding).refresh.finishLoadMoreWithNoMoreData();
                    }
                } else if (VideoFragment.this.page == 1) {
                    ((FragmentVideoBinding) VideoFragment.this.binding).statusLayout.showError();
                } else {
                    ToastUtils.getInstance().showWeak(baseRes.getMsg());
                }
            }
        });
        this.modelPlayer.dataBuyVideo().observe(this, new Observer<BaseRes<Integer>>() {
            /* class com.androidx.p013lv.invention.fragment.VideoFragment.C11576 */

            public void onChanged(BaseRes<Integer> baseRes) {
                if (baseRes.getCode() == 200) {
                    ((VideoBean) VideoFragment.this.adapter.getDataInPosition(VideoFragment.this.position)).setBuy(true);
                    if (baseRes.getData().intValue() == 1) {
                        ToastUtils.getInstance().showCorrect("購買成功");
                        VideoFragment.this.userAccount.setGold(VideoFragment.this.userAccount.getGold() - VideoFragment.this.perice);
                        SpUtils.getInstance().setUserAccount(VideoFragment.this.userAccount);
                    } else {
                        ToastUtils.getInstance().showCorrect("租賃成功");
                        VideoFragment.this.userAccount.setGold(VideoFragment.this.userAccount.getGold() - VideoFragment.this.perice);
                        SpUtils.getInstance().setUserAccount(VideoFragment.this.userAccount);
                    }
                    if (VideoFragment.this.videoPlayer != null) {
                        VideoFragment.this.videoPlayer.setIsBuy(true);
                        return;
                    }
                    return;
                }
                ToastUtils.getInstance().showSigh(baseRes.getMsg());
            }
        });
        getInfo();
        ((FragmentVideoBinding) this.binding).recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            /* class com.androidx.p013lv.invention.fragment.VideoFragment.C11587 */

            @Override // androidx.recyclerview.widget.RecyclerView.OnScrollListener
            public void onScrollStateChanged(RecyclerView recyclerView, int i) {
                super.onScrollStateChanged(recyclerView, i);
                if (i != 0) {
                    return;
                }
                if (VideoFragment.this.isOnClick) {
                    VideoFragment.this.isOnClick = false;
                } else {
                    VideoFragment.this.autoPlayVideo(recyclerView);
                }
            }

            @Override // androidx.recyclerview.widget.RecyclerView.OnScrollListener
            public void onScrolled(RecyclerView recyclerView, int i, int i2) {
                super.onScrolled(recyclerView, i, i2);
                VideoFragment videoFragment = VideoFragment.this;
                videoFragment.firstVisibleItem = videoFragment.linearLayoutManager.findFirstVisibleItemPosition();
                VideoFragment videoFragment2 = VideoFragment.this;
                videoFragment2.lastVisibleItem = videoFragment2.linearLayoutManager.findLastVisibleItemPosition();
                VideoFragment videoFragment3 = VideoFragment.this;
                videoFragment3.visibleCount = videoFragment3.lastVisibleItem - VideoFragment.this.firstVisibleItem;
                VideoFragment.access$508(VideoFragment.this);
            }
        });
    }

    /* access modifiers changed from: package-private */
    public void startAd(AdInfoBean adInfoBean) {
        LogUtils.m63e("dianad", "点击广告");
        if (adInfoBean.getJumpType() == 2) {
            try {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setData(Uri.parse(adInfoBean.getAdJump()));
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            if (this.appLink == null) {
                this.appLink = new APPLink(getActivity());
            }
            this.appLink.urlLink(adInfoBean.getAdJump());
        }
        Intent intent2 = new Intent(getActivity(), AdClickService.class);
        intent2.putExtra("adId", adInfoBean.getAdId());
        getActivity().startService(intent2);
    }

    /* access modifiers changed from: package-private */
    public void getInfo() {
        if (this.page == 1) {
            AdapterVideo adapterVideo = this.adapter;
            if (!(adapterVideo == null || adapterVideo.getData() == null || this.adapter.getData().size() <= 0)) {
                this.adapter.clear();
            }
            if (!NetUtil.isNetworkAvailable()) {
                ((FragmentVideoBinding) this.binding).statusLayout.showNoNet();
                return;
            }
            ((FragmentVideoBinding) this.binding).statusLayout.showLoading();
        }
        this.model.queryBrushStation();
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        AdapterVideo adapterVideo = this.adapter;
        if (adapterVideo == null || adapterVideo.getData() == null || this.adapter.getData().size() == 0) {
            ((FragmentVideoBinding) this.binding).refresh.finishLoadMore();
        } else {
            getInfo();
        }
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnRefreshListener
    public void onRefresh(RefreshLayout refreshLayout) {
        this.adapter.setIsPlay(1);
        this.data = null;
        this.page = 1;
        this.count = 0;
        getInfo();
    }

    @Override // com.androidx.p013lv.player.view.BrushVideoPlayer.PlayerVideoInterface
    public void leaseVideo(BrushVideoPlayer brushVideoPlayer, VideoBean videoBean, int i) {
        this.videoPlayer = brushVideoPlayer;
        this.position = i;
        btnBuyOrLease(videoBean, 3);
    }

    @Override // com.androidx.p013lv.player.view.BrushVideoPlayer.PlayerVideoInterface
    public void buyVideo(BrushVideoPlayer brushVideoPlayer, VideoBean videoBean, int i) {
        this.videoPlayer = brushVideoPlayer;
        this.position = i;
        btnBuyOrLease(videoBean, 1);
    }

    @Override // com.androidx.p013lv.player.view.BrushVideoPlayer.PlayerVideoInterface
    public void buyVip(BrushVideoPlayer brushVideoPlayer, VideoBean videoBean, int i) {
        ARouter.getInstance().build(Constance.VIP_CENTER_ACTIVITY).withInt("data", 5).navigation();
    }

    private void btnBuyOrLease(VideoBean videoBean, int i) {
        this.perice = videoBean.getPrice();
        if (this.userAccount.getGold() < this.perice) {
            ToastUtils.getInstance().showSigh("金幣不足，請充值");
            ARouter.getInstance().build(Constance.VIP_CENTER_ACTIVITY).withInt("type", 1).withInt("data", 5).navigation();
            return;
        }
        this.modelPlayer.buyOrLeaseVideo(videoBean.getVideoId(), i, true, getContext());
    }

    @Override // com.androidx.p013lv.player.view.BrushVideoPlayer.PlayerVideoInterface
    public void click(BrushVideoPlayer brushVideoPlayer, VideoBean videoBean, int i, int i2) {
        this.player = brushVideoPlayer;
        if (i2 == 0 && !this.isResume) {
            brushVideoPlayer.onVideoPause();
        }
        if (i2 == 1) {
            this.isOnClick = true;
        }
        LogUtils.m63e("点击播放", "position:" + i + "<---->top:" + this.top);
        if (this.itemHeight == 0) {
            this.itemHeight = UiUtils.dp2px(52) + ((UiUtils.getWindowWidth() * 9) / 16);
        }
        int i3 = (i - this.index) * this.itemHeight;
        this.index = i;
        if (i3 != 0) {
            ((FragmentVideoBinding) this.binding).recycler.smoothScrollBy(0, i3, new LinearInterpolator(), CropImageView.DEFAULT_IMAGE_TO_CROP_BOUNDS_ANIM_DURATION);
        }
        if (i3 == 0) {
            this.isOnClick = false;
            if (videoBean.isAd()) {
                startAd(videoBean.getAdInfoBean());
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void autoPlayVideo(RecyclerView recyclerView) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        int i = 0;
        for (int i2 = 0; i2 < this.visibleCount; i2++) {
            if (!(layoutManager == null || layoutManager.getChildAt(i2) == null || ((View) Objects.requireNonNull(layoutManager.getChildAt(i2))).findViewById(C1069R.C1072id.play) == null)) {
                BrushVideoPlayer brushVideoPlayer = (BrushVideoPlayer) ((View) Objects.requireNonNull(layoutManager.getChildAt(i2))).findViewById(C1069R.C1072id.play);
                Rect rect = new Rect();
                brushVideoPlayer.getLocalVisibleRect(rect);
                int height = brushVideoPlayer.getHeight();
                if (rect.top == 0 && rect.bottom == height && i == 0) {
                    i++;
                    this.top = layoutManager.getChildAt(i2).getTop();
                    if (brushVideoPlayer.getCurrentState() == 0 || brushVideoPlayer.getCurrentState() == 7 || brushVideoPlayer.getCurrentState() == 6 || brushVideoPlayer.getCurrentState() == 5) {
                        brushVideoPlayer.startPlayLogic();
                        this.player = brushVideoPlayer;
                        this.index = brushVideoPlayer.getPosition();
                    }
                }
            }
        }
    }
}
